import { setToken, getToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken(),
  userInfo: {},
  permissions: {}
}

const mutations = {
  // 保存token
  saveToken(state, token) {
    state.token = token
    setToken(token)
  },
  // 删除token
  REMOVE_TOKEN(state) {
    state.token = ''
    removeToken()
  },
  // 保存用户信息
  saveUserInfo(state, obj) {
    state.userInfo = obj
  },
  // 清空用户信息
  REMOVE_USERINFO(state) {
    state.userInfo = {}
  },
  // 保存前用户权限
  savePermissionsP(state, permissions) {
    state.permissions = permissions
  },
  // 清空用户路由
  REMOVE_ROUTER(state) {
    state.permissions = {}
  }
}

const actions = {
  // 退出登录
  logout(store) {
    store.commit('REMOVE_TOKEN')
    store.commit('REMOVE_USERINFO')
    store.commit('REMOVE_ROUTER')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
